/*
 * Copyright 2008. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the Apache License, Version 2.0. You
 * can find a copy of the license at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

package com.sinai.mshab.extensions.search;

import java.util.ArrayList;

/**
 * Parses the UI objects of an advanced search and creates list of
 * {@link Source} objects to represent the data which was requested.
 * 
 * @author serge
 * 
 */
public class AdvancedSearchParser {

	private ArrayList<String> columns;
	
	public AdvancedSearchParser() {

	}
	
	public ArrayList<String> getColumns() {
		return columns;
	}

	/**
	 * Creates sources and constraints from the categories and field
	 * 
	 * @param adv
	 */
	public void parse(AdvancedSearch adv) {
		
		//TODO: decide on how AdvancedSearch will work.
		
		// set up our data storage variables.
		columns = new ArrayList<String>();
//		ArrayList<Source> sources = new ArrayList<Source>();
		ArrayList<SearchCategory> categories = adv.getCategories();
		
		// create key source
		String table_name = categories.get(0).getSelectedTable();
//		Source keySource = new Source();
//		keySource.setMajorIdentifier(table_name);
//		keySource.setMinorIdentifier(adv.getKeyField());
//		sources.add(keySource);
		columns.add(adv.getKeyField());

		// going through all the categories
		for (int i = 0; i < categories.size(); i++) {
			SearchCategory category = categories.get(i);
			String table = category.getSelectedTable();

			// going through the conditions of given category
			ArrayList<SearchField> conditions = category.getConditions();
			for (int k = 0; k < conditions.size(); k++) {
				SearchField cond = conditions.get(k);
				String field = cond.getField();
				String operator = cond.getOperator();
				String value = cond.getValue();

				System.out.println(table + "." + field + " " + operator + " "
						+ value);
				
				// add the columns 
				columns.add(field);

				// set up the source
//				Source current_source = new Source();
//				current_source.setMajorIdentifier(table);
//				current_source.setMinorIdentifier(field);

				// set up the constraint
//				ConstraintImpl<String> constraint = new ConstraintImpl<String>(
//						table, field, value, operator);
//				current_source.addConstraint(constraint);

				// adding the source to our list.
//				sources.add(current_source);
			}
		}
	}
}
